class StartSwim extends eui.Component implements  eui.UIComponent {
	public constructor() {
		super();
		var skinName = "StartSwimSkin";
	}

	protected partAdded(partName:string,instance:any):void
	{
		super.partAdded(partName,instance);
	}

	private leftright;//左右键
	private bgAni;//背景图
	private bgAlpAni;
	private startAni;//开始秒数
	private bgA;
	private bgA2;
	private safeline1;
	private safeline2;
	private mline1;
	private mline2;
	private mline3;
	private mline4;
	private wall;//墙
	private win;//win字样
	public speed:number = 0;//游泳线的额外加速度
	private v:number = 0;
	private totalv:number = 35;//最大速度
	private mc;
	private mcDataFactory;
	private long: number = 0;
	private totalLong: number = 2000;
	private m:string;//分钟
	private s:string;//秒钟
	private curT;//时间
	private tipT;//提示语
	private swimer;//人物容器
	private winMusic = new egret.Sound();//结束音乐容器
	private winUrl = "resource/assets/lottery/8804.wav";//结束音乐
	private shuiMusic = new egret.Sound();//游泳水声
	private shuiUrl = "resource/assets/StartSwim/swimming.wav"
	
	

	//绘制时间显示区
	private time(){
        let timeS = new egret.Sprite();
        this.addChild(timeS);
		this.curT = createText("00:00",30,0xffffff);
        timeS.addChild(this.curT);
        timeS.anchorOffsetX = timeS.width>>1;
        timeS.x= this.stage.stageWidth>>1;
        timeS.y = 50;
	}
	//提示语
	private tip(){
		let text = new egret.Sprite();
        this.addChildAt(text,2);
		this.tipT = createText("点击左右键，向前游动",20,0xffffff);
        text.addChild(this.tipT);
        text.anchorOffsetX = text.width>>1;
        text.x= this.stage.stageWidth>>1;
        text.y = 1070;
	}

	protected childrenCreated():void
	{
		super.childrenCreated();
		this.winMusic.load(this.winUrl);
		
		this.mline2.height = 0;//让游泳线一开始先消失
		
		//左右键
		this.playAnimation(this.leftright, false);
		this.tip();
		
		//加载人物
		this.mcDataFactory = new egret.MovieClipDataFactory(RES.getRes("swim_json"), RES.getRes("swim_png"));
       	let mcData = this.mcDataFactory.generateMovieClipData("ready"); 
       	this.mc = new egret.MovieClip(mcData);
      	this.swimer.addChild(this.mc);
	   	this.swimer.anchorOffsetX = this.mc.width>>1;
	   	this.swimer.x = this.stage.stageWidth>>1;
	   	this.swimer.y = 240;

		//泡泡
		this.playAnimation(this.bgAlpAni, true);

		this.time();//时间区
		this.playAnimation(this.startAni, false);//准备时间
		
		//正式开始
		let delayT = egret.setTimeout(function(){
			this.tipT.text = "";
			this.swimming();
		},this,3750);
	}

	private swimming(){
		//时间计时器
		let timeS:egret.Timer = new egret.Timer(100,-1);
		let tempS = 0,  tempS0 = 0;//秒
		let tempM = 0,  tempM0 = 0;//分
		timeS.addEventListener(egret.TimerEvent.TIMER,function(){
			tempS ++;
			if(tempS >= 10){
				tempS0 ++ ;
				tempS = 0;
			}
			if(tempS0 >= 6){
				tempS0 = 0;
				tempM ++;
				if(tempM >= 10){
					tempM0 ++ ;
					tempM = 0;
				}
			}
			this.s = tempS0.toString() + tempS.toString();
			this.m = tempM0.toString() + tempM.toString();
			this.curT.text = this.m + ":" + this.s;
		},this);
		timeS.start();

		//点击游泳
		this.touchEnabled = true;
		this.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
			if(this.long < this.totalLong){
				if(this.v >= this.totalv){
					this.v = this.totalv;
				}else{
					this.v += 6;
				}
				this.long = this.long + this.v;
			}
		}, this)

		//监听状态 
		let first = true;
		this.addEventListener(egret.Event.ENTER_FRAME, function(){
			if(this.long >= this.totalLong){//停止
				this.winMusic.play(0,1);//从哪开始播放，播放次数，小于等于0==循环播放
				timeS.stop();
				this.win.y = 310;
				egret.Tween.get(this.win, {loop: true}).to({scaleX:1.1, scaleY: 1.1}, 800);
				this.v = 0;
				this.wall.y = 0;
				this.mline2.y = -1136;
				this.mline3.y = -1136;
				this.mline4.y = 370;
				this.bgA.y = 0;
				this.bgA2.y = -1136;
				this.mc.movieClipData = this.mcDataFactory.generateMovieClipData("ready");
			}else{
				if(this.v>0){//惯性
					this.shuiMusic.play(0,1);
					this.v -=.5;
					this.mc.frameRate = this.v;
					if(first){
						first = false;
						this.mc.movieClipData = this.mcDataFactory.generateMovieClipData("swim");
						this.mc.play(-1);
					}
					
					this.bgMove();
				}else{
					first = true;
					this.mc.movieClipData = this.mcDataFactory.generateMovieClipData("ready");
				}
			}
		},this)
	}
	//线移动 
		private bgMove(){
			this.safeline1.y += this.v;
			this.safeline2.y += this.v;
			this.mline2.y += this.v;
			this.mline3.y += this.v;
			this.bgA.y += this.v;
			this.bgA2.y += this.v;
			//安全栏
			if (this.safeline1.y >= 1136) {
				this.safeline1.y = -(this.safeline2.height - this.safeline2.y);
			}
			if (this.safeline2.y >= 1136) {
				this.safeline2.y = -(this.safeline1.height - this.safeline1.y);
			}
			if(this.mline1.y < 1136){
				this.mline1.y += this.v;
			}else{
				this.mline2.height = 1136;
			}
			//游泳线
			if(this.mline2.y >= 1136){
				this.mline2.y = -this.mline3.height + this.mline3.y;
			}
			if(this.mline3.y >= 1136){
				this.mline3.y = -this.mline2.height + this.mline2.y;
			}
			//背景图
			if(this.bgA.y >= 1136){
				this.bgA.y = -1136 + this.bgA2.y;
			}
			if(this.bgA2.y >= 1136){
				this.bgA2.y = -1136 + this.bgA.y;
			}
		}
	//设置循环播放
	private playAnimation(target: egret.tween.TweenGroup, isLoop: boolean): void {
		if (isLoop) {
			for (var key in target.items) {
				target.items[key].props = { loop: true };
			}
		}
		target.play();
	}
	
}